<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib  prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib  prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="srcPath" value="${redis:getSysConfig('sysFileVisitRoot_back')}"/>
<c:set var="uploadPath" value="${redis:getSysConfig('uploadFileVisitRoot')}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>抢购商品信息</title>
    <jsp:include page="../util/import.jsp"/>
    <script type="text/javascript">
        Date.prototype.format = function (format) {
            var o = {
                "M+": this.getMonth() + 1, //month
                "d+": this.getDate(), //day
                "h+": this.getHours(), //hour
                "m+": this.getMinutes(), //minute
                "s+": this.getSeconds(), //second
                "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
                "S": this.getMilliseconds() //millisecond
            };;

            if (/(y+)/.test(format)) {
                format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            }

            for (var k in o) {
                if (new RegExp("(" + k + ")").test(format)) {
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
                }
            }
            return format;
        };


        $(function(){
            var $tt= $("#tt").bootstrapTable({
                url: '${basePath}/back/panicBuying/listPanicBuying.sjson ',         	/**请求后台的URL（*）**/
                method: 'post',                       	/**请求方式（*）**/
                contentType: "application/x-www-form-urlencoded",
                dataType: 'json',
                toolbar: '#toolbar',               	    /**工具按钮用哪个容器**/
                striped: true,                      	/**是否显示行间隔色**/
                cache: false,                       	/**是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）**/
                pagination: true,                   	/**是否显示分页（*）**/
                sortable: false,                    	/**是否启用排序**/
                sortOrder: 'asc',                   	/**排序方式**/
                queryParamsType: '',
                sidePagination: 'server',           	/**分页方式：client客户端分页，server服务端分页（*）**/
                pageNumber: 1,
                pageSize:10,							/**初始化加载第一页，默认第一页**/
                pageList: [5,10, 25, 50],        	    /**可供选择的每页的行数（*）**/
                search: false,                       	/**是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大**/
                strictSearch: true,
                showColumns: true,                  	/**是否显示所有的列**/
                showRefresh: true,                  	/**是否显示刷新按钮**/
                minimumCountColumns: 2,             	/**最少允许的列数**/
                clickToSelect: true,                	/**是否启用点击选中行**/
                uniqueId: 'panicId',                    /**每一行的唯一标识，一般为主键列**/
                singleSelect  : true,                   /** 单选checkbox**/
                showToggle:false,                    	/**是否显示详细视图和列表视图的切换按钮**/
                cardView: false,                    	/**是否显示详细视图**/
                detailView: false,                  	/**是否显示父子表**/
                columns: [
                    {checkbox: true},
                    {field: 'Number', title: '编号',align: 'center',valign: 'middle',width:'50px',
                        formatter: function (value, row, index) {
                            var page = $tt.bootstrapTable("getPage");
                            return page.pageSize * (page.pageNumber - 1) + index + 1;

                        }
                    },
                    {field: 'logoImg',title: '抢购图片',align: 'center',valign: 'middle',
                        formatter: function (value, row, index) {
                            var a_img_html = '<div><a  data-toggle="modal" data-target="#detailModal" href="#" mce_href="#" onclick="showListRebackImage(' + index + ',1,1),detailInfo(' + row.panicId + ')"><img id="uploadImg_' + index + '" class="uploadImg" onMouseOver="showListBigImage(' + index + ',4,4,0,-40)" onMouseOut="showListRebackImage(' + index + ',1,1)"  src="${uploadPath}/' + row.logoImg + '" onerror="this.src=\'${srcPath}/common/images/mrbj.png\'"  width="30px" height="30px"/></a></div>';
                            return a_img_html;
                        }
                    },
                    {field: 'panicTitle',title: '抢购标题',align: 'center',valign: 'middle',
                        formatter:function(value,row,index){
                            var  a_html = '<a data-toggle="modal" data-target="#detailModal" href="#" mce_href="#" onclick="detailInfo('+row.panicId+')">' + value + '</a>';
                            return a_html;
                        }},
                    {field: 'panicId',title: '抢购编号',align: 'center',valign: 'middle'},
                    {field: 'productId',title: '商品主Id',align: 'center',valign: 'middle'},
                    {field: 'panicPrice',title: '抢购价',align: 'center',valign: 'middle'},
                    {field: 'panicNum',title: '抢购个数',align: 'center',valign: 'middle'},
                    {field: 'buyNum',title: '已购个数',align: 'center',valign: 'middle'},
                    {field: 'startTime',title: '抢购开始时间',align: 'center',valign: 'middle',
                        formatter:function(value,row,index){
                            var d2 = new Date().format("yyyy-MM-dd hh:mm:ss");
                            value=toJsonDate(value,"yyyy-MM-dd hh:mm:ss");;
                            if (value - d2 < 0) {
                                return "<span style='color:red'>" + value + "</span>";
                            } else {
                                return value;
                            }
                        }
                    },
                    {field: 'activityTime',title: '抢购持续时间(小时)',align: 'center',valign: 'middle'},
                    {field: 'isPublish',title: '是否发布',align: 'center',valign: 'middle',
                            formatter:function(value,row,index){
                                if (value == "0") { return "<font class='color_002'>未发布</font>";  }
                                if (value == "1") { return "<font class='color_001'>已发布</font>"; }
                        }
                    },
                    {field: 'isOver',title: '是否结束',align: 'center',valign: 'middle',
                        formatter:function(value,row,index){
                            if (value == "0") { return "<font class='color_001'>未结束</font>";  }
                            if (value == "1") { return "<font class='color_002'>已结束</font>"; }
                        }
                    },
                    {field: 'isShow',title: '抢购首页是否显示',align: 'center',valign: 'middle',
                        formatter:function(value,row,index){
                            if (value == "0") { return "<font class='color_002'>不显示</font>";  }
                            if (value == "1") { return "<font class='color_001'>显示</font>"; }
                        }
                    }
                ],
                queryParams: function (params) {
                    /**特别说明，返回的参数的值为空，则当前参数不会发送到服务器端,这种指定请求参数的方式和datatables控价类似**/
                    params.panicTitle = $("#q_panicTitle").val();
                    params.isPublish = $("#q_isPublish").val();
                    params.isOver = $("#q_isOver").val();
                    params.isShow = $("#q_isShow").val();
                    params.currentPage = params.pageNumber;
                    return params;
                }
            });
        });


        /**
         * 删除抢购商品
         */
        <shiro:hasPermission name="back_gotoPanicBuyingPage_delete">
        function deleteInfo(){
            /**获取选中行的id**/
            var selectedRow= $("#tt").bootstrapTable('getSelections');
            if(selectedRow.length==1){
                var panicId=selectedRow[0].panicId;
                var isPublish=selectedRow[0].isPublish;
                var isOver=selectedRow[0].isOver;
                if (isPublish == 0 || isOver==1) {
                    showConfirm("你确定要删除吗?", function() {
                        $.ajax({
                            type: "POST",
                            dataType: "JSON",
                            url: "${basePath}/back/panicBuying/deletePanicBuying.sjson",
                            data: {panicId:panicId,isPublish:isPublish,isOver:isOver},
                            success: function(data){
                                if(data.success==true){
                                    $("#tt").bootstrapTable('refresh');
                                }else{
                                    showMsg("删除失败！");
                                }
                            }
                        });
                    });
                }else{
                    showMsg("正在抢购，不可删除！");
                }
            }else{
                showMsg("请选择一行记录！");
            }
        }
        </shiro:hasPermission>

        <shiro:hasPermission name="back_gotoPanicBuyingPage_over">
        function overInfo(){
            /**获取选中行的id**/
            var selectedRow= $("#tt").bootstrapTable('getSelections');
            if(selectedRow.length==1){
                var panicId=selectedRow[0].panicId;
                var isPublish=selectedRow[0].isPublish;
                var isOver=selectedRow[0].isOver;
                if (isPublish == 1 && isOver==0) {
                    showConfirm("确定要结束抢购吗?", function() {
                        $.ajax({
                            type: "POST",
                            dataType: "JSON",
                            url: "${basePath}/back/panicBuying/closePanicBuying.sjson",
                            data: {panicId:panicId,isPublish:isPublish,isOver:isOver},
                            success: function(data){
                                if(data.success==true){
                                    showMsg("结束抢购成功！");
                                    $("#tt").bootstrapTable('refresh');
                                }else{
                                    showMsg("结束抢购失败！");
                                }
                            }
                        });
                    });
                }else if (isPublish == 0 && isOver==0){
                    showMsg("未发布抢购不可结束！");
                }else if (isPublish == 1 && isOver==1){
                    showMsg("抢购已经结束！！");
                }
            }else{
                showMsg("请选择一行记录！");
            }
        }
        </shiro:hasPermission>

        <shiro:hasPermission name="back_gotoPanicBuyingPage_publish">
        function publish(){
            /**获取选中行的id**/
            var selectedRow= $("#tt").bootstrapTable('getSelections');
            if(selectedRow.length==1){
                var panicId=selectedRow[0].panicId;
                var isPublish=selectedRow[0].isPublish;
                if (isPublish == 0) {
                    showConfirm("确定要发布抢购吗?", function() {
                    $.ajax({
                        type: "POST",
                        dataType: "JSON",
                        url: "${basePath}/back/panicBuying/updatePublish.sjson",
                        data: {panicId: panicId},
                        success: function (data) {
                            if (data.msg == "true") {
                                showMsg("抢购发布成功！");
                                $("#tt").bootstrapTable('refresh');
                            } else if (data.msg == "false1") {
                                showMsg("该抢购商品已有参与其他抢购");
                            } else if (data.msg == "false") {
                                showMsg("抢购发布失败！");
                                }
                            }
                        });
                    });
                }else{
                showMsg("该抢购已经发布！");
                }
            }else{
                showMsg("请选择一行记录！");
            }
        }
        </shiro:hasPermission>

        <shiro:hasPermission name="back_gotoPanicBuyingPage_recommend">
        function recommend(){
            var selectedRow= $("#tt").bootstrapTable('getSelections');
            if(selectedRow.length==1){
                var panicId=selectedRow[0].panicId;
                var isShow =selectedRow[0].isShow;
                if(isShow==0){
                    showConfirm("确定要设置显示首页推荐吗?", function () {
                    $.ajax({
                        type: "POST",
                        dataType: "JSON",
                        url: "${basePath}/back/panicBuying/updateIndexShow.sjson",
                        data: {panicId: panicId},
                        success: function (data) {
                            if (data.success ==true) {
                                showMsg("推荐栏显示成功！");
                                $("#tt").bootstrapTable('refresh');
                            } else if (data.success ==false) {
                                showMsg("推荐栏显示失败！");
                            }
                        }
                    });
                });
               }
            }
        }
        </shiro:hasPermission>
    </script>

</head>
<body>
    <!-- 查询框  -->
    <div class="panel-body" id="customer-main-panel">
        <!-- 查询条件 -->
        <div class="panel panel-default">
            <div class="panel-heading">抢购信息查询</div>
            <div class="panel-body">
                <form id="qform" class="form-horizontal">
                    <div class="form-group" style="margin-top:15px">
                        <div class="col-sm-3" >
                            <div class="input-group input-group-sm">
                                <span class="input-group-addon">抢购标题</span>
                                <input type="text" class="form-control" id="q_panicTitle" name="panicTitle" placeholder="请输入抢购标题"/>
                            </div>
                        </div>
                        <div class="col-sm-2" style="width: 200px;">
                            <div class="input-group input-group-sm">
                                <span class="input-group-addon">是否发布</span>
                                <select id="q_isPublish" class="form-control">
                                    <option value="">请选择</option>
                                    <option value="0">未发布</option>
                                    <option value="1">已发布</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-2" style="width: 200px;">
                            <div class="input-group input-group-sm">
                                <span class="input-group-addon">是否结束</span>
                                <select id="q_isOver" class="form-control">
                                    <option value="">请选择</option>
                                    <option value="0">未结束</option>
                                    <option value="1">已结束</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-2" style="width: 200px;">
                            <div class="input-group input-group-sm">
                                <span class="input-group-addon">是否显示</span>
                                <select id="q_isShow" class="form-control">
                                    <option value="">请选择</option>
                                    <option value="0">不显示</option>
                                    <option value="1">显示</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <button type="button" id="btn_query" class="btn btn-primary">查询</button>&nbsp;&nbsp;
                            <button type="reset" class="btn btn-success" id="resetBtn">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <!-- 工具栏 -->
        <div id="toolbar" class="btn-group">
            <shiro:hasPermission name="back_gotoPanicBuyingPage_add">
                <button id="btn_add" type="button" class="btn btn-default" data-toggle="modal" data-target="#addOrEditModal" onclick="addInfo()">
                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>添加
                </button>
            </shiro:hasPermission>
            <shiro:hasPermission name="back_gotoPanicBuyingPage_update">
                <button id="btn_edit" type="button" class="btn btn-default" data-toggle="modal" onclick="editPanicInfo()">
                    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
                </button>
            </shiro:hasPermission>
            <shiro:hasPermission name="back_gotoPanicBuyingPage_over">
                <button id="btn_over" type="button" class="btn btn-default" data-toggle="modal" onclick="overInfo()">
                    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>结束
                </button>
            </shiro:hasPermission>
            <shiro:hasPermission name="back_gotoPanicBuyingPage_delete">
                <button id="btn_delete" type="button" class="btn btn-default" onclick="deleteInfo()">
                    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
                </button>
            </shiro:hasPermission>
            <shiro:hasPermission name="back_gotoPanicBuyingPage_publish">
                <button id="btn_publish" type="button" class="btn btn-default" onclick="publish()">
                    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>发布
                </button>
            </shiro:hasPermission>
            <%-- TODO 暂时隐藏 --%>
            <%--<shiro:hasPermission name="back_gotoPanicBuyingPage_recommend">
                <button id="btn_recommend" type="button" class="btn btn-default" onclick="recommend()">
                    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>设置显示首页推荐栏
                </button>
            </shiro:hasPermission>--%>
            <shiro:hasPermission name="back_gotoPanicBuyingPage_panic">
                <button id="btn_panic" type="button" class="btn btn-default" data-toggle="modal" onclick="listPanicBuyingRecord()">
                    <span class="glyphicon glyphicon-search" aria-hidden="true"></span>查看商品抢购记录
                </button>
            </shiro:hasPermission>
        </div>
        <table id="tt"></table>
        <!--商品抢购记录-->
        <jsp:include page="panicBuyingRecordList.jsp"/>
        <!--选择商品-->
        <jsp:include page="addProductList.jsp"/>
        <!--添加或修改抢购-->
        <jsp:include page="addOrEditor.jsp"/>
        <%--抢购详情--%>
        <jsp:include page="panicBuyingdetail.jsp"/>
    </div>
</body>
</html>
